/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.DAO;

import com.aptech.model.postType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Hoang Quy Hong An
 */
public class postTypeDAO {

    private Connection myCon = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;
    private String strCommand = "";

    public ArrayList<postType> getAllPostType() {
        
        ArrayList<postType> listPostType = new ArrayList<postType>();
        postType objPostType = null;
        strCommand = "SELECT postTypeID ,postTypeName ,postTypePrice FROM tbl_PostType WHERE postTypeStatus = 1";
        try {
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = ps.executeQuery();
            
            while (rs.next()) {
                System.out.println("Co ra ko ?");
                objPostType = new postType();
                int postTypeID = rs.getInt("postTypeID");
                String postTypeName = rs.getString("postTypeName");
                String postTypePrice = rs.getString("postTypePrice");

                objPostType.setPostTypeID(postTypeID);
                objPostType.setPostTypeName(postTypeName);
                objPostType.setPostTypePrice(postTypePrice);
                listPostType.add(objPostType);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listPostType;
    }
    public String getPostTypePriceByID(int postTypeID) {
        
        String postTypePrice = "";
        strCommand = "SELECT postTypePrice FROM tbl_PostType WHERE postTypeID = ?";
        System.out.println("Command: "+ strCommand);
        System.out.println("PostypeID: "+postTypeID);
        try {
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, postTypeID);
            rs = ps.executeQuery();
            while (rs.next()) {
                postTypePrice = rs.getString("postTypePrice");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return postTypePrice;
    }
}
